package com.logistics.Action;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.DAO.Action;
import com.DAO.User;
import com.opensymphony.xwork2.ActionSupport;

public class Deleter extends ActionSupport implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	/**
	 * @title Manager.java
	 * @package com.logistics.Action
	 * @author ufo
	 */

	private Map<String,String> result=new HashMap<String,String>();
	private HttpServletRequest request;
	private String[] id;
	private String[] currentDate;//形式均为yyyy-mm-dd hh:mm:ss
	
	protected Configuration conf=new Configuration().configure();
	protected SessionFactory sf=conf.buildSessionFactory();
	protected Session hibernateSess=sf.openSession();
	protected Transaction tx = hibernateSess.beginTransaction();
	protected User user;
	protected Action action;

	public String delete(){
		if(isOnline()&&getCurrentDate()[0]!=null)
		{
			addSessionToUser();
			if(user.getFlag()==1){
				try{
				action=(Action)hibernateSess.load(Action.class, getId()[0]);
				}
				catch(Exception e){
					result.put("flag", "false");
					return SUCCESS;
				}
				action.setCheckDate(getCurrentDate()[0]);
				action.setEndDate(getCurrentDate()[0]);
				action.setStartDate(getCurrentDate()[0]);

				hibernateSess.save(action);
				tx.commit();
				result.put("flag", "true");
				return SUCCESS;
			}
		}
		result.put("flag", "false");
		return SUCCESS;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO 自动生成的方法存根
		this.request=request;
	}
	@JSON(name="result")
	public Map<String,String> getResult() {
		return result;
	}
	public void setResult(Map<String,String> result) {
		this.result = result;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public String[] getId() {
		return id;
	}
	public void setId(String[] id) {
		this.id = id;
	}
	public String[] getCurrentDate() {
		return currentDate;
	}
	public void setStartDate(String[] currentDate) {
		this.currentDate = currentDate;
	}
	private void addSessionToUser(){
		user=(User) hibernateSess.load(User.class, (Serializable) request.getSession().getAttribute("userId"));
	}
	public boolean isOnline(){
		if(request.getSession().getAttribute("userId")!=null)
			return true;
		else 
			return false;
	}
}
